package com.tsd.jxc.setting.service.impl;

import cn.hutool.db.Entity;
import com.alibaba.fastjson.JSON;
import com.tsd.core.cache.CacheOperatorApi;
import com.tsd.core.datasource.DataSourceContextHolder;
import com.tsd.core.utils.FlywayUtil;
import com.tsd.core.utils.HlpUtils;
import com.tsd.jxc.setting.service.JxcSettingService;
import com.tsd.jxc.vo.JxcSettingVo;
import com.tsd.system.dao.SqlSelectMapper;
import com.tsd.system.entity.SysSettingExt;
import com.tsd.system.service.SysSettingService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: JxcSettingServiceImpl
 * @Description: TODO
 * @Author: Hillpool
 * @Date: 2022/11/9 18:09
 * @Version: 1.0
 */
@Service
public class JxcSettingServiceImpl implements JxcSettingService {

    @Resource
    private SysSettingService sysSettingService;
    @Resource
    private CacheOperatorApi<JxcSettingVo> jxcSettingCacheOperatorApi;

    @Resource
    private SqlSelectMapper sqlSelectMapper;

    @Override
    public JxcSettingVo getJxcSettingVo(String accountSetSid) throws Exception {
        JxcSettingVo settingVo = jxcSettingCacheOperatorApi.get(accountSetSid);
        if (settingVo == null) {
            SysSettingExt query = new SysSettingExt();
            query.setCode(JxcSettingVo.JXC_SETTING_CODE);
            query.setDr(0);
            SysSettingExt sysSetting = sysSettingService.getSysSettingByCodeOwnerSid(query);
            if (sysSetting != null) {
                settingVo = JSON.parseObject(sysSetting.getJson(), JxcSettingVo.class);
                settingVo.setId(sysSetting.getId());
                settingVo.setSid(sysSetting.getSid());
                jxcSettingCacheOperatorApi.put(accountSetSid, settingVo);
            } else {
                settingVo = new JxcSettingVo();
                settingVo.setCode(JxcSettingVo.JXC_SETTING_CODE);
            }
        }
        return settingVo;
    }


    @Override
    public void clearJxcSettingVoCache(String accountSetSid) {
        jxcSettingCacheOperatorApi.remove(accountSetSid);
    }

    @Override
    public void dataInit(String type) {
        // free-免费版，basic-基础版，pro-专业版，ultimate-旗舰版
        List<String> locations = new ArrayList<>();
        locations.add("classpath:db-init/base");
        Entity entity = DataSourceContextHolder.getAccountSet();
        if (!HlpUtils.isEmpty(type) && "demo".equals(type)) {
            String version = entity.getStr("version");
            if (HlpUtils.isEmpty(version)) {
                version = "free";
            }
            locations.add(String.format("classpath:db-init/%s", version));
        }
        // 1、清除已执行的sql文件
        DataSource dataSource = DataSourceContextHolder.getDataSource();
        FlywayUtil.clearExistRunSql4DataInit(dataSource, locations);
        // 2、执行初始化文件
        FlywayUtil.doFlyway4DbInit(dataSource, entity, locations.toArray(new String[0]));
    }
}
